'''
The notes application
'''

def start(args):
    import os
    
    loader = args['loader']

    router = loader.get_component('server.WSGI_Server').get_router()
    wrap = loader.get_component('utils.wrap')
    
    path, file = os.path.split(__file__)
    js_file = os.path.join(path, 'main.js')
    
    db = loader.get_component('database.documents')
    app_db = db.create_app_database('notes')
    
    rpc = loader.get_component('rpc')
    
    @rpc.function
    def save_notes(data):
        app_db.save('notes', data)
        return True
        
    @rpc.function
    def get_notes():
        return app_db.get('notes')
    
    @router.route('/notes/main')
    def main(header, request, response_headers):
        with open(js_file, 'r') as f:
            js = f.read()
        return wrap(js)
    
def stop(args):
    router = loader.get_component('wsgi_server.WSGI_Server').get_router()
    router.delete_route('/notes/main')
